package org.dromara.business.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.business.domain.bo.PostBo;
import org.dromara.business.domain.vo.PostVo;
import org.dromara.business.service.PostService;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 评论
 *
 * @author Lion Li
 * @date 2025-03-20
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/post")
public class PostController extends BaseController {

    private final PostService postService;

    /**
     * 查询评论列表
     */
//    @SaCheckPermission("business:post:list")
    @GetMapping("/list")
    public TableDataInfo<PostVo> list(PostBo bo, PageQuery pageQuery) {
        return postService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出评论列表
     */
    @SaCheckPermission("business:post:export")
    @Log(title = "评论", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PostBo bo, HttpServletResponse response) {
        List<PostVo> list = postService.queryList(bo);
        ExcelUtil.exportExcel(list, "评论", PostVo.class, response);
    }

    /**
     * 获取评论详细信息
     *
     * @param postId 主键
     */
    @SaCheckPermission("business:post:query")
    @GetMapping("/{postId}")
    public R<PostVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long postId) {
        return R.ok(postService.queryById(postId));
    }

    /**
     * 新增评论
     */
//    @SaCheckPermission("business:post:add")
//    @Log(title = "评论", businessType = BusinessType.INSERT)
    @RepeatSubmit(interval = 1000)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PostBo bo) {
        return toAjax(postService.insertByBo(bo));
    }

    /**
     * 修改评论
     */
    @SaCheckPermission("business:post:edit")
    @Log(title = "评论", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PostBo bo) {
        return toAjax(postService.updateByBo(bo));
    }

    /**
     * 删除评论
     *
     * @param postIds 主键串
     */
    @SaCheckPermission("business:post:remove")
    @Log(title = "评论", businessType = BusinessType.DELETE)
    @DeleteMapping("/{postIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] postIds) {
        return toAjax(postService.deleteWithValidByIds(List.of(postIds), true));
    }
}
